package com.gxg.newdemo1.mynewdamo1.signature;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.os.Environment;

public class FileUtils {


	private static final String SDPATH = Environment.getExternalStorageDirectory() + "/";

	public static String getSDPATH()
	{
		return SDPATH;
	}

	public static String getCacheDir(Context env)
	{
		return env.getCacheDir() + "/";
	}

	/**
	 * 创建SD卡目录
	 * @param dirName
	 */
	public static File creatSDDir(String dirName)
	{

		File file = new File(SDPATH + dirName);

		file.mkdirs();

		return file;

	}

	/**
	 * 
	 * 创建SD文件
	 * 
	 * 
	 * 
	 * @throws IOException
	 */

	public static File creatSDFile(String fileName) throws IOException
	{

		File file = new File(SDPATH + fileName);

		file.createNewFile();

		return file;

	}

	/**
	 * 
	 * 判断SD文件是否存在？
	 */
	public static boolean isFileExist(String fileName)
	{

		File file = new File(SDPATH + fileName);

		return file.exists();
		

	}

	public static File write2SDFromInputTest(InputStream inputStream, String path,
			String fileName)
	{
		File file = null;
		OutputStream outputStream = null;
		
		try
		{
			
			creatSDDir(path);
			file = creatSDFile(path + fileName);
			outputStream = new FileOutputStream(file);
			
			int tmp;
			while((tmp = inputStream.read())!= -1)
			{
				outputStream.write(tmp);
			}
			outputStream.flush();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			
			try
			{
				if(null != outputStream)
				{
					outputStream.close();
				}
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		
		return file;
	
	}
	
	public static void deleteFile(String path)
	{
		if(null == path)
			return;
		
		File f = new File(path);
		f.delete();
	}
	
	public static String readByteArrayToPath(String path) throws IOException{

		File file = new File(path);  
		FileInputStream is = new FileInputStream(file);  
		byte[] b = new byte[is.available()];  
		is.read(b);  
		String result = new String(b);  
		is.close();
		return result;
		
	}
	
	public static File writeByteArrayToPath(String path, byte[] input)
	{
		FileOutputStream fos = null;
		try
		{
			File f = new File(path);
			
			if(!f.exists())
			{
				f.createNewFile();
			}
			
			if(!f.isFile())
			{
				return null;
			}
			else
			{
				fos = new FileOutputStream(f);
				fos.write(input);
				fos.flush();
				fos.close();
				return f;
			}
		}
		catch (Exception e)
		{
			return null;
		}
		finally
		{
			try
			{
				if(null != fos)
					fos.close();
			}
			catch (IOException e)
			{
				//do nothing
			}
		}
	}
	
	/**
	 * 
	 * 从InputStream写输入到SD卡上
	 */

	public static File write2SDFromInput(InputStream inputStream, String path,
			String fileName)
	{
		File file = null;
		OutputStream outputStream = null;
		try
		{
			creatSDDir(path);
			file = creatSDFile(path + fileName);
			outputStream = new FileOutputStream(file);
			byte buffer[] = new byte[1024];
			while ((inputStream.read(buffer)) != -1)
			{
				outputStream.write(buffer);
			}
			outputStream.flush();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				outputStream.close();
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		return file;
	}
}
